package com.lvy.hczn.front.business.controller;

import com.lvy.hczn.front.business.common.Constants;
import com.lvy.hczn.front.system.domain.business.SortTaskDetail;
import com.lvy.hczn.front.business.dto.SortTaskDetailDto;
import com.lvy.hczn.front.business.service.SortTaskDetailService;
import com.lvy.hczn.front.business.util.ResultUtil;
import com.lvy.hczn.front.common.exception.UtilException;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/wcs/sort/detail/")
@RestController
@Slf4j
public class SortTaskDetailController {

    @Autowired
    private SortTaskDetailService sortTaskDetailService;

    @ApiOperation(value = "累加已分拣总重量", notes = "累加已分拣总重量")
    @PostMapping("updateSortWeightById")
    private ResultUtil updateSortWeightById(HttpServletRequest request, @RequestBody SortTaskDetailDto sortTaskDetail) {
        Map<String, Object> map = new HashMap<>();
        try {
            map = sortTaskDetailService.updateSortWeightById(request, sortTaskDetail, sortTaskDetail.getBoxCode(), sortTaskDetail.getSortIds());
            return ResultUtil.ok(map);
        } catch (Exception e) {
            log.error("updateSortWeightById:{}", e);
            map.put("inError", true);
            if (e instanceof UtilException) {
                return ResultUtil.failed(map, e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }

    @ApiOperation(value = "地平仓累加已分拣总重量", notes = "地平仓累加已分拣总重量")
    @PostMapping("updateSortWeightByIdOfLarge")
    private ResultUtil updateSortWeightByIdOfLarge(HttpServletRequest request, @RequestBody SortTaskDetailDto sortTaskDetail) {
        Map<String, Object> map = new HashMap<>();
        try {
            map = sortTaskDetailService.updateSortWeightByIdOfLarge(request, sortTaskDetail);
            return ResultUtil.ok(map);
        } catch (Exception e) {
            log.error("updateSortWeightByIdOfLarge:", e);
            map.put("inError", true);
            if (e instanceof UtilException) {
                return ResultUtil.failed(map, e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }

    @ApiOperation(value = "分拣中的任务", notes = "分拣中的任务")
    @PostMapping("selectSortingTaskDetail")
    private ResultUtil selectSortingTaskDetail(@RequestBody SortTaskDetailDto sortTaskDetail) {
        try {
            if (sortTaskDetail.getIds() == null || sortTaskDetail.getIds().isEmpty()) {
                return ResultUtil.failed("原料为空");
            }
            List<SortTaskDetail> list = new ArrayList<>();
            for (String spuId : sortTaskDetail.getIds()) {
                List<SortTaskDetail> sortTaskDetailList = sortTaskDetailService.selectByClient(sortTaskDetail.getClientId(), Constants.SORT_TASK_DETAIL_ING, spuId, sortTaskDetail.getBatchCode(), null);
                if (!sortTaskDetailList.isEmpty()) {
                    list.addAll(sortTaskDetailList);
                }
            }

            return ResultUtil.ok(list);
        } catch (Exception e) {
            log.error("selectSortingTaskDetail:{}", e);
            if (e instanceof UtilException) {
                return ResultUtil.failed(e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }

    @ApiOperation(value = "根据ID更改状态（如缺货）", notes = "根据ID更改状态（如缺货）")
    @PostMapping("updateStatusById")
    private ResultUtil updateStatusById(@RequestBody SortTaskDetailDto sortTaskDetail) {
        try {
//            sortTaskDetail.setId("1684402792867909634");
//            sortTaskDetail.setStatus("2");
//            sortTaskDetail.setSortTaskId("1684402792867909633");
            sortTaskDetailService.updateStatusById(sortTaskDetail, null);
            return ResultUtil.ok();
        } catch (Exception e) {
            log.error("updateStatusById:{}", e);
            if (e instanceof UtilException) {
                return ResultUtil.failed(e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }
}
